/*
 *    Copyright 2010 Vanessa Williams
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package bioformatica.symbol;

/**
 * Nucleotide alphabet, including ambiguous bases
 *
 * @author vwilliam
 */
public enum Base {

    /**
     * Adenine
     */
    A("Adenine"),
    /**
     * Cytosine
     */
    C("Cytosine"),
    /**
     * Guanine
     */
    G("Guanine"),
    /**
     * Thymine
     */
    T("Thymine"),

    /**
     * Uracil
     */
    U("Uracil"),

    /**
     * aNy
     */
    N("aNy"),
    /**
     * gap
     */
    X("gap"),

    /**
     * puRine - A or G
     */
    R("puRine - A or G"),
    /**
     * pYrimidine - C or T
     */
    Y("pYrimidine - C or T"),
    /**
     * aMino - A or C
     */
    M("aMino - A or C"),
    /**
     * G or T
     */
    K("G or T"),
    /**
     * C or G
     */
    S("C or G"),
    /**
     * A or T
     */
    W("A or T")  
    ;

    private char c;
    private byte b;
    private String baseName;

    /**
     * Constructor
     * @param name name of the Base, e.g. Adenine
     */
    Base(String name) {
        this.c = this.name().toCharArray()[0];
        this.b = this.name().getBytes()[0];
    }

    /**
     *
     * @return the base as a char
     */
    public char getChar() {
        return this.c;
    }

    /**
     *
     * @return the base as a byte
     */
    public byte getByte() {
        return this.b;
    }

    /**
     * Convert byte to Base
     *
     * @param b byte to convert
     * @return Base represented by b
     */
    public static Base getBase(byte b) {
        if (Base.A.getByte() == b) return Base.A;
        if (Base.C.getByte() == b) return Base.C;
        if (Base.G.getByte() == b) return Base.G;
        if (Base.T.getByte() == b) return Base.T;
        if (Base.N.getByte() == b) return Base.N;
        if (Base.R.getByte() == b) return Base.R;
        if (Base.Y.getByte() == b) return Base.Y;
        if (Base.M.getByte() == b) return Base.M;
        if (Base.K.getByte() == b) return Base.K;
        if (Base.S.getByte() == b) return Base.S;
        if (Base.W.getByte() == b) return Base.W;
        if (Base.X.getByte() == b) return Base.X;
        return null;
    }
}
